#!/usr/bin/python
# -*- coding: UTF-8 -*-

#  demo of the usage of python
#
#
#

"""Test code

Suggested usage: import MyUnrar
"""
__decription__ = "Batch Unrar Kit"
__version__ = "0.1"
__date__ = " Mar.28th.2007"
__author__ = "hu <huys03@gmail.com> "
__license__ = "Licensed under the GPLv2, see the file LICENSE in this tarball."
__copyright__= "Copyright (C) 2007 by hu <huys03@gmail.com>."

from time import sleep
import string
import os
from os.path import join, getsize, abspath, exists, splitext

__WinRAR__="C:\\Program Files\\WinRAR\\WinRAR.exe"

exec_rar= '\"'+__WinRAR__ + '\"'
urar_chm =""" IF EXIST *.rar """ + exec_rar + """  e -o- -y *.rar *.chm"""
urar_pdf = """IF EXIST *.rar """ + exec_rar + """  e -o- -y *.rar *.pdf"""
urar_rar = """IF EXIST *.rar """ + exec_rar + """  e -o- -y *.rar *.rar"""

class MyUnrar:
###
    def __init__ (self, path=os.getcwd()):
        self.path = path
        self.count = 0

###
    def checkRAR(self):
        if exists(__WinRAR__):
            print "WinRAR is OK!"
        else:
            print "WinRAR is not there!"


###
    def unrar(self):
        print self.count
        for f in os.listdir( self.path ) :
            (filename, ext) = splitext(f)
            if ext.lower() == ".zip":
                command =exec_rar + ' e -o- -y ' + f + ' *.r*'
                os.system( command )

            if ext.lower() == ".rar":
                os.system( urar_chm )
                os.system( urar_pdf )
                os.system( urar_rar )
        self.count = self.count + 1

    def unrar(self, path):
        print self.count
        for f in os.listdir( path ) :
            (filename, ext) = splitext(f)
            if ext.lower() == ".zip":
                command =exec_rar +' e -o- -y ' + path + '\\' + f + ' *.r*'
            #    print command
                os.system( command )

            if ext.lower() == ".rar":
                os.system( urar_chm )
                os.system( urar_pdf )
                os.system( urar_rar )
        self.count = self.count + 1

    def deleteRAR(self):
        for f in os.listdir( self.path ) :
            (filename, ext) = splitext(f)
            if ext.lower().find(".r") != -1:
                os.remove(f)

###
    def run(self):
        print "==============================="
        print __decription__
        print 'version:', __version__
        print __license__
        print __copyright__
        print "==============================="
        self.checkRAR();

        for root, dirs, files in os.walk( self.path ):
            for name in dirs:
                    fullpath = join(root, name)
                    print fullpath
                    self.unrar(fullpath)
                    os.system( urar_chm )
                    os.system( urar_pdf )
                    self.deleteRAR()



###
if __name__=="__main__":
    ur = MyUnrar()
    ur.run()
    sleep(5)
